package com.zhoug.chart3.utils;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;

import java.util.List;

/**
 * 计算缩放
 * @Author: zhoug
 * @Date: 2024-02-01
 * @Description:
 */
public class MPLineBarScaleHelper {
    private int oneScreenMinNum = 6;//一屏最少显示多少个柱子,即:放大到此时就不能放大了
    private boolean autoScaleMax = true;//自动缩放到最大缩放值,即一屏显示oneScreenMinNum
    private boolean autoSetYMaxValue = true;//自动计算设置左坐标y轴最大值,防止y轴显示的label值小于实际值


    /**
     * 一屏最少显示多少个柱子,即:放大到此时就不能放大了
     *
     * @param oneScreenMinNum 默认6
     */
   
    public void setOneScreenMinNum(int oneScreenMinNum) {
        this.oneScreenMinNum = oneScreenMinNum;
    }
   
    /**
     * 自动缩放到最大缩放值
     *
     * @param autoScaleMax 默认 true
     */
   
    public void setAutoScaleMax(boolean autoScaleMax) {
        this.autoScaleMax = autoScaleMax;
    }


    /**
     * 自动计算设置左坐标y轴最大值,防止y轴显示的label值小于实际值
     *
     * @param autoSetYMaxValue 默认 true
     */
   
    public void setAutoSetYMaxValue(boolean autoSetYMaxValue) {
        this.autoSetYMaxValue = autoSetYMaxValue;
    }

    /**
     *
     * 在{@link BarChart#setData(ChartData)}{@link LineChart#setData(ChartData)}中调用
     * @param barLineChartBase
     * @param data
     */
    public void calculateScale(BarLineChartBase<?> barLineChartBase, BarLineScatterCandleBubbleData<?> data) {
        if(barLineChartBase==null || data==null)return;
        if (oneScreenMinNum > 0) {
            //计算出柱子最大条数
            int entryCount = 0;
            float maxY = 0;//y轴最大值
            List<?> dataSets = data.getDataSets();
            if (dataSets != null && dataSets.size() > 0) {
                for (Object dataSet : dataSets) {
                    if (dataSet instanceof IDataSet) {
                        IDataSet<?> iDataSet = (IDataSet<?>) dataSet;
                        entryCount = Math.max(iDataSet.getEntryCount(), entryCount);
                        if (autoSetYMaxValue && iDataSet.getAxisDependency() == YAxis.AxisDependency.LEFT) {
                            int count = iDataSet.getEntryCount();
                            if (count > 0) {
                                for (int i = 0; i < count; i++) {
                                    Entry entry = iDataSet.getEntryForIndex(i);
                                    if (entry != null) {
                                        maxY = Math.max(entry.getY(), maxY);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (entryCount > 0) {
                float axisMaxScale = entryCount / (float) oneScreenMinNum;
                barLineChartBase.getViewPortHandler().setMaximumScaleX(axisMaxScale);
                if (autoScaleMax) {
                    //自动缩放到最大缩放值,即一屏显示oneScreenMinNum
                    barLineChartBase.zoom(axisMaxScale, 1, 0, 0);
                }
            }
            /*if (autoSetLeftYAxisMaxValue && maxY > 0) {
                ChartUtils.setYAxisMaxValue(barLineChartBase.getAxisLeft(), maxY);
            }*/
        }
    }
}
